<!DOCTYPE html>
<html >

<head>

  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <title>Python3学习教程</title>
  <meta name="description" content="Python3学习教程">
  <meta name="generator" content="bookdown 0.5 and GitBook 2.6.7">

  <meta property="og:title" content="Python3学习教程" />
  <meta property="og:type" content="book" />
  
  
  
  

  <meta name="twitter:card" content="summary" />
  <meta name="twitter:title" content="Python3学习教程" />
  
  
  

<meta name="author" content="易生信培训团队">
<meta name="author" content="联系我们: train@ehbio.com">
<meta name="author" content="抱歉，转成PDF后部分格式问题还未解决，会继续更新调整">


<meta name="date" content="2018-05-09">

  <meta name="viewport" content="width=device-width, initial-scale=1">
  <meta name="apple-mobile-web-app-capable" content="yes">
  <meta name="apple-mobile-web-app-status-bar-style" content="black">
  
  
<link rel="prev" href="Python-plot-WXN.html">
<link rel="next" href="py3-pandas-ct.html">
<script src="libs/jquery-2.2.3/jquery.min.js"></script>
<link href="libs/gitbook-2.6.7/css/style.css" rel="stylesheet" />
<link href="libs/gitbook-2.6.7/css/plugin-bookdown.css" rel="stylesheet" />
<link href="libs/gitbook-2.6.7/css/plugin-highlight.css" rel="stylesheet" />
<link href="libs/gitbook-2.6.7/css/plugin-search.css" rel="stylesheet" />
<link href="libs/gitbook-2.6.7/css/plugin-fontsettings.css" rel="stylesheet" />









<style type="text/css">
div.sourceCode { overflow-x: auto; }
table.sourceCode, tr.sourceCode, td.lineNumbers, td.sourceCode {
  margin: 0; padding: 0; vertical-align: baseline; border: none; }
table.sourceCode { width: 100%; line-height: 100%; }
td.lineNumbers { text-align: right; padding-right: 4px; padding-left: 4px; color: #aaaaaa; border-right: 1px solid #aaaaaa; }
td.sourceCode { padding-left: 5px; }
code > span.kw { color: #007020; font-weight: bold; } /* Keyword */
code > span.dt { color: #902000; } /* DataType */
code > span.dv { color: #40a070; } /* DecVal */
code > span.bn { color: #40a070; } /* BaseN */
code > span.fl { color: #40a070; } /* Float */
code > span.ch { color: #4070a0; } /* Char */
code > span.st { color: #4070a0; } /* String */
code > span.co { color: #60a0b0; font-style: italic; } /* Comment */
code > span.ot { color: #007020; } /* Other */
code > span.al { color: #ff0000; font-weight: bold; } /* Alert */
code > span.fu { color: #06287e; } /* Function */
code > span.er { color: #ff0000; font-weight: bold; } /* Error */
code > span.wa { color: #60a0b0; font-weight: bold; font-style: italic; } /* Warning */
code > span.cn { color: #880000; } /* Constant */
code > span.sc { color: #4070a0; } /* SpecialChar */
code > span.vs { color: #4070a0; } /* VerbatimString */
code > span.ss { color: #bb6688; } /* SpecialString */
code > span.im { } /* Import */
code > span.va { color: #19177c; } /* Variable */
code > span.cf { color: #007020; font-weight: bold; } /* ControlFlow */
code > span.op { color: #666666; } /* Operator */
code > span.bu { } /* BuiltIn */
code > span.ex { } /* Extension */
code > span.pp { color: #bc7a00; } /* Preprocessor */
code > span.at { color: #7d9029; } /* Attribute */
code > span.do { color: #ba2121; font-style: italic; } /* Documentation */
code > span.an { color: #60a0b0; font-weight: bold; font-style: italic; } /* Annotation */
code > span.cv { color: #60a0b0; font-weight: bold; font-style: italic; } /* CommentVar */
code > span.in { color: #60a0b0; font-weight: bold; font-style: italic; } /* Information */
</style>

<link rel="stylesheet" href="style.css" type="text/css" />
</head>

<body>



  <div class="book without-animation with-summary font-size-2 font-family-1" data-basepath=".">

    <div class="book-summary">
      <nav role="navigation">

<ul class="summary">
<li><a href="http://www.ehbio.com"><img src="http://www.ehbio.com/logos/ehbio_gitbook_logo.png" width="95%"></a></li>

<li class="divider"></li>
<li class="chapter" data-level="" data-path="index.html"><a href="index.html"><i class="fa fa-check"></i>EHBIO Gene Technology</a></li>
<li class="chapter" data-level="1" data-path="pythonbasic.html"><a href="pythonbasic.html"><i class="fa fa-check"></i><b>1</b> Python基础</a><ul>
<li class="chapter" data-level="1.1" data-path="pythonbasic.html"><a href="pythonbasic.html#section-1.1"><i class="fa fa-check"></i><b>1.1</b> 交互模式下表达式</a></li>
<li class="chapter" data-level="1.2" data-path="pythonbasic.html"><a href="pythonbasic.html#pythonintfloatstr"><i class="fa fa-check"></i><b>1.2</b> Python中的数据类型：整数（int）、浮点（float）和字符串（str）</a></li>
<li class="chapter" data-level="1.3" data-path="pythonbasic.html"><a href="pythonbasic.html#section-1.3"><i class="fa fa-check"></i><b>1.3</b> 字符串的连接和复制</a></li>
<li class="chapter" data-level="1.4" data-path="pythonbasic.html"><a href="pythonbasic.html#section-1.4"><i class="fa fa-check"></i><b>1.4</b> 变量</a></li>
<li class="chapter" data-level="1.5" data-path="pythonbasic.html"><a href="pythonbasic.html#helloworld.py"><i class="fa fa-check"></i><b>1.5</b> 第一小程序HelloWorld.py</a></li>
<li class="chapter" data-level="1.6" data-path="pythonbasic.html"><a href="pythonbasic.html#section-1.6"><i class="fa fa-check"></i><b>1.6</b> 逻辑和比较操作</a><ul>
<li class="chapter" data-level="1.6.1" data-path="pythonbasic.html"><a href="pythonbasic.html#section-1.6.1"><i class="fa fa-check"></i><b>1.6.1</b> 布尔逻辑值</a></li>
<li class="chapter" data-level="1.6.2" data-path="pythonbasic.html"><a href="pythonbasic.html#section-1.6.2"><i class="fa fa-check"></i><b>1.6.2</b> 比较操作符</a></li>
<li class="chapter" data-level="1.6.3" data-path="pythonbasic.html"><a href="pythonbasic.html#-and-or-not"><i class="fa fa-check"></i><b>1.6.3</b> 布尔操作符 and or not</a></li>
<li class="chapter" data-level="1.6.4" data-path="pythonbasic.html"><a href="pythonbasic.html#section-1.6.4"><i class="fa fa-check"></i><b>1.6.4</b> 布尔操作和比较操作符</a></li>
</ul></li>
<li class="chapter" data-level="1.7" data-path="pythonbasic.html"><a href="pythonbasic.html#section-1.7"><i class="fa fa-check"></i><b>1.7</b> 控制流</a><ul>
<li class="chapter" data-level="1.7.1" data-path="pythonbasic.html"><a href="pythonbasic.html#if-"><i class="fa fa-check"></i><b>1.7.1</b> if 语句</a></li>
<li class="chapter" data-level="1.7.2" data-path="pythonbasic.html"><a href="pythonbasic.html#elif-"><i class="fa fa-check"></i><b>1.7.2</b> elif 否则如果</a></li>
<li class="chapter" data-level="1.7.3" data-path="pythonbasic.html"><a href="pythonbasic.html#while"><i class="fa fa-check"></i><b>1.7.3</b> while循环</a></li>
<li class="chapter" data-level="1.7.4" data-path="pythonbasic.html"><a href="pythonbasic.html#breakcontinue"><i class="fa fa-check"></i><b>1.7.4</b> break和continue</a></li>
<li class="chapter" data-level="1.7.5" data-path="pythonbasic.html"><a href="pythonbasic.html#for--range"><i class="fa fa-check"></i><b>1.7.5</b> for 和 range（）函数</a></li>
<li class="chapter" data-level="1.7.6" data-path="pythonbasic.html"><a href="pythonbasic.html#range"><i class="fa fa-check"></i><b>1.7.6</b> range()函数（开始，停止，步长）</a></li>
</ul></li>
<li class="chapter" data-level="1.8" data-path="pythonbasic.html"><a href="pythonbasic.html#section-1.8"><i class="fa fa-check"></i><b>1.8</b> 导入模块</a></li>
<li class="chapter" data-level="1.9" data-path="pythonbasic.html"><a href="pythonbasic.html#-"><i class="fa fa-check"></i><b>1.9</b> 函数： 内置函数、自定义函数</a><ul>
<li class="chapter" data-level="1.9.1" data-path="pythonbasic.html"><a href="pythonbasic.html#printlen-input-intstr"><i class="fa fa-check"></i><b>1.9.1</b> 函数print（），len （）,input （），int（），str（）均为内置函数</a></li>
</ul></li>
<li class="chapter" data-level="1.10" data-path="pythonbasic.html"><a href="pythonbasic.html#section-1.10"><i class="fa fa-check"></i><b>1.10</b> 局部和全局作用域</a></li>
<li class="chapter" data-level="1.11" data-path="pythonbasic.html"><a href="pythonbasic.html#global"><i class="fa fa-check"></i><b>1.11</b> 声明为全局变量global</a></li>
<li class="chapter" data-level="1.12" data-path="pythonbasic.html"><a href="pythonbasic.html#section-1.12"><i class="fa fa-check"></i><b>1.12</b> 异常的处理</a></li>
<li class="chapter" data-level="1.13" data-path="pythonbasic.html"><a href="pythonbasic.html#section-1.13"><i class="fa fa-check"></i><b>1.13</b> 列表</a></li>
<li class="chapter" data-level="1.14" data-path="pythonbasic.html"><a href="pythonbasic.html#section-1.14"><i class="fa fa-check"></i><b>1.14</b> 字符串和元组</a></li>
<li class="chapter" data-level="1.15" data-path="pythonbasic.html"><a href="pythonbasic.html#section-1.15"><i class="fa fa-check"></i><b>1.15</b> 引用</a></li>
<li class="chapter" data-level="1.16" data-path="pythonbasic.html"><a href="pythonbasic.html#--"><i class="fa fa-check"></i><b>1.16</b> 字典 键：值 对</a></li>
<li class="chapter" data-level="1.17" data-path="pythonbasic.html"><a href="pythonbasic.html#section-1.17"><i class="fa fa-check"></i><b>1.17</b> 字典与列表</a><ul>
<li class="chapter" data-level="1.17.1" data-path="pythonbasic.html"><a href="pythonbasic.html#keysvaluesitems"><i class="fa fa-check"></i><b>1.17.1</b> keys()、values（）和items( )</a></li>
</ul></li>
<li class="chapter" data-level="1.18" data-path="pythonbasic.html"><a href="pythonbasic.html#section-1.18"><i class="fa fa-check"></i><b>1.18</b> 字符串操作</a></li>
</ul></li>
<li class="chapter" data-level="2" data-path="py3-ct.html"><a href="py3-ct.html"><i class="fa fa-check"></i><b>2</b> Python 教程</a><ul>
<li class="chapter" data-level="2.1" data-path="py3-ct.html"><a href="py3-ct.html#section-2.1"><i class="fa fa-check"></i><b>2.1</b> 背景介绍</a><ul>
<li class="chapter" data-level="2.1.1" data-path="py3-ct.html"><a href="py3-ct.html#section-2.1.1"><i class="fa fa-check"></i><b>2.1.1</b> 编程开篇</a></li>
<li class="chapter" data-level="2.1.2" data-path="py3-ct.html"><a href="py3-ct.html#python"><i class="fa fa-check"></i><b>2.1.2</b> 为什么学习Python</a></li>
<li class="chapter" data-level="2.1.3" data-path="py3-ct.html"><a href="py3-ct.html#python"><i class="fa fa-check"></i><b>2.1.3</b> Python常用包</a></li>
<li class="chapter" data-level="2.1.4" data-path="py3-ct.html"><a href="py3-ct.html#python"><i class="fa fa-check"></i><b>2.1.4</b> 怎么学习Python</a></li>
<li class="chapter" data-level="2.1.5" data-path="py3-ct.html"><a href="py3-ct.html#python"><i class="fa fa-check"></i><b>2.1.5</b> Python学习的几个阶段</a></li>
<li class="chapter" data-level="2.1.6" data-path="py3-ct.html"><a href="py3-ct.html#python"><i class="fa fa-check"></i><b>2.1.6</b> 如何安装Python</a></li>
<li class="chapter" data-level="2.1.7" data-path="py3-ct.html"><a href="py3-ct.html#python"><i class="fa fa-check"></i><b>2.1.7</b> 如何运行Python命令和脚本</a></li>
<li class="chapter" data-level="2.1.8" data-path="py3-ct.html"><a href="py3-ct.html#python"><i class="fa fa-check"></i><b>2.1.8</b> 使用什么编辑器写Python脚本</a></li>
</ul></li>
<li class="chapter" data-level="2.2" data-path="py3-ct.html"><a href="py3-ct.html#python"><i class="fa fa-check"></i><b>2.2</b> Python程序事例</a></li>
<li class="chapter" data-level="2.3" data-path="py3-ct.html"><a href="py3-ct.html#python"><i class="fa fa-check"></i><b>2.3</b> Python语法</a><ul>
<li class="chapter" data-level="2.3.1" data-path="py3-ct.html"><a href="py3-ct.html#section-2.3.1"><i class="fa fa-check"></i><b>2.3.1</b> 层级缩进</a></li>
<li class="chapter" data-level="2.3.2" data-path="py3-ct.html"><a href="py3-ct.html#python"><i class="fa fa-check"></i><b>2.3.2</b> Python作为计算器的使用</a></li>
<li class="chapter" data-level="2.3.3" data-path="py3-ct.html"><a href="py3-ct.html#section-2.3.3"><i class="fa fa-check"></i><b>2.3.3</b> 变量、数据结构、流程控制</a></li>
</ul></li>
<li class="chapter" data-level="2.4" data-path="py3-ct.html"><a href="py3-ct.html#section-2.4"><i class="fa fa-check"></i><b>2.4</b> 输入输出</a><ul>
<li class="chapter" data-level="2.4.1" data-path="py3-ct.html"><a href="py3-ct.html#section-2.4.1"><i class="fa fa-check"></i><b>2.4.1</b> 交互式输入输出</a></li>
<li class="chapter" data-level="2.4.2" data-path="py3-ct.html"><a href="py3-ct.html#section-2.4.2"><i class="fa fa-check"></i><b>2.4.2</b> 文件读写</a></li>
</ul></li>
<li class="chapter" data-level="2.5" data-path="py3-ct.html"><a href="py3-ct.html#section-2.5"><i class="fa fa-check"></i><b>2.5</b> 实战练习（一）</a><ul>
<li class="chapter" data-level="2.5.1" data-path="py3-ct.html"><a href="py3-ct.html#section-2.5.1"><i class="fa fa-check"></i><b>2.5.1</b> 背景知识</a></li>
<li class="chapter" data-level="2.5.2" data-path="pythonbasic.html"><a href="pythonbasic.html#-"><i class="fa fa-check"></i><b>2.5.2</b> 作业 (一)</a></li>
</ul></li>
<li class="chapter" data-level="2.6" data-path="py3-ct.html"><a href="py3-ct.html#section-2.6"><i class="fa fa-check"></i><b>2.6</b> 函数操作</a><ul>
<li class="chapter" data-level="2.6.1" data-path="py3-ct.html"><a href="py3-ct.html#section-2.6.1"><i class="fa fa-check"></i><b>2.6.1</b> 作业（二）</a></li>
</ul></li>
<li class="chapter" data-level="2.7" data-path="py3-ct.html"><a href="py3-ct.html#section-2.7"><i class="fa fa-check"></i><b>2.7</b> 模块</a></li>
<li class="chapter" data-level="2.8" data-path="py3-ct.html"><a href="py3-ct.html#section-2.8"><i class="fa fa-check"></i><b>2.8</b> 命令行参数</a><ul>
<li class="chapter" data-level="2.8.1" data-path="py3-ct.html"><a href="py3-ct.html#section-2.8.1"><i class="fa fa-check"></i><b>2.8.1</b> 作业（三）</a></li>
</ul></li>
<li class="chapter" data-level="2.9" data-path="py3-ct.html"><a href="py3-ct.html#python"><i class="fa fa-check"></i><b>2.9</b> 更多Python内容</a><ul>
<li class="chapter" data-level="2.9.1" data-path="py3-ct.html"><a href="py3-ct.html#section-2.9.1"><i class="fa fa-check"></i><b>2.9.1</b> <strong>单语句块</strong></a></li>
<li class="chapter" data-level="2.9.2" data-path="py3-ct.html"><a href="py3-ct.html#section-2.9.2"><i class="fa fa-check"></i><b>2.9.2</b> 列表解析</a></li>
<li class="chapter" data-level="2.9.3" data-path="py3-ct.html"><a href="py3-ct.html#section-2.9.3"><i class="fa fa-check"></i><b>2.9.3</b> 字典解析</a></li>
<li class="chapter" data-level="2.9.4" data-path="py3-ct.html"><a href="py3-ct.html#section-2.9.4"><i class="fa fa-check"></i><b>2.9.4</b> 断言</a></li>
<li class="chapter" data-level="2.9.5" data-path="py3-ct.html"><a href="py3-ct.html#section-2.9.5"><i class="fa fa-check"></i><b>2.9.5</b> 更多字符串方法</a></li>
<li class="chapter" data-level="2.9.6" data-path="py3-ct.html"><a href="py3-ct.html#lambda-map-filer-reduce-"><i class="fa fa-check"></i><b>2.9.6</b> lambda, map, filer, reduce (保留节目)</a></li>
<li class="chapter" data-level="2.9.7" data-path="py3-ct.html"><a href="py3-ct.html#exec-eval-python-"><i class="fa fa-check"></i><b>2.9.7</b> <strong>exec, eval (执行字符串python语句, 保留节目)</strong></a></li>
<li class="chapter" data-level="2.9.8" data-path="py3-ct.html"><a href="py3-ct.html#section-2.9.8"><i class="fa fa-check"></i><b>2.9.8</b> 正则表达式</a></li>
</ul></li>
<li class="chapter" data-level="2.10" data-path="py3-ct.html"><a href="py3-ct.html#python"><i class="fa fa-check"></i><b>2.10</b> Python画图</a></li>
<li class="chapter" data-level="2.11" data-path="py3-ct.html"><a href="py3-ct.html#reference"><i class="fa fa-check"></i><b>2.11</b> Reference</a></li>
</ul></li>
<li class="chapter" data-level="3" data-path="Python-plot-WXN.html"><a href="Python-plot-WXN.html"><i class="fa fa-check"></i><b>3</b> Python作图</a><ul>
<li class="chapter" data-level="3.1" data-path="Python-plot-WXN.html"><a href="Python-plot-WXN.html#section-3.1"><i class="fa fa-check"></i><b>3.1</b> 绘图基础</a><ul>
<li class="chapter" data-level="3.1.1" data-path="Python-plot-WXN.html"><a href="Python-plot-WXN.html#figuresubplot-1"><i class="fa fa-check"></i><b>3.1.1</b> Figure和Subplot</a></li>
<li class="chapter" data-level="3.1.2" data-path="Python-plot-WXN.html"><a href="Python-plot-WXN.html#subplot-1"><i class="fa fa-check"></i><b>3.1.2</b> 调整subplot周围间距</a></li>
<li class="chapter" data-level="3.1.3" data-path="Python-plot-WXN.html"><a href="Python-plot-WXN.html#--1"><i class="fa fa-check"></i><b>3.1.3</b> 颜色 标记和线型</a></li>
<li class="chapter" data-level="3.1.4" data-path="py3-ct.html"><a href="py3-ct.html#-1"><i class="fa fa-check"></i><b>3.1.4</b> 刻度、标签和图例</a></li>
<li class="chapter" data-level="3.1.5" data-path="Python-plot-WXN.html"><a href="Python-plot-WXN.html#legend-1"><i class="fa fa-check"></i><b>3.1.5</b> 添加图例legend</a></li>
<li class="chapter" data-level="3.1.6" data-path="py3-ct.html"><a href="py3-ct.html#-1"><i class="fa fa-check"></i><b>3.1.6</b> 注解</a></li>
<li class="chapter" data-level="3.1.7" data-path="py3-ct.html"><a href="py3-ct.html#-1"><i class="fa fa-check"></i><b>3.1.7</b> 图片保存</a></li>
</ul></li>
<li class="chapter" data-level="3.2" data-path="Python-plot-WXN.html"><a href="Python-plot-WXN.html#section-3.2"><i class="fa fa-check"></i><b>3.2</b> 绘图实例</a><ul>
<li class="chapter" data-level="3.2.1" data-path="py3-ct.html"><a href="py3-ct.html#-1"><i class="fa fa-check"></i><b>3.2.1</b> 绘制散点图</a></li>
<li class="chapter" data-level="3.2.2" data-path="py3-ct.html"><a href="py3-ct.html#-1"><i class="fa fa-check"></i><b>3.2.2</b> 折线图</a></li>
<li class="chapter" data-level="3.2.3" data-path="py3-ct.html"><a href="py3-ct.html#-1"><i class="fa fa-check"></i><b>3.2.3</b> 直方图</a></li>
<li class="chapter" data-level="3.2.4" data-path="py3-ct.html"><a href="py3-ct.html#-1"><i class="fa fa-check"></i><b>3.2.4</b> 直条图</a></li>
<li class="chapter" data-level="3.2.5" data-path="py3-ct.html"><a href="py3-ct.html#-1"><i class="fa fa-check"></i><b>3.2.5</b> 箱线图</a></li>
<li class="chapter" data-level="3.2.6" data-path="py3-ct.html"><a href="py3-ct.html#-1"><i class="fa fa-check"></i><b>3.2.6</b> 饼图</a></li>
<li class="chapter" data-level="3.2.7" data-path="Python-plot-WXN.html"><a href="Python-plot-WXN.html#section-3.2.7"><i class="fa fa-check"></i><b>3.2.7</b> 绘制基因矩阵的热图</a></li>
</ul></li>
</ul></li>
<li class="chapter" data-level="4" data-path="Py3-pratcise-ct.html"><a href="Py3-pratcise-ct.html"><i class="fa fa-check"></i><b>4</b> Python实战</a><ul>
<li class="chapter" data-level="4.1" data-path="py3-ct.html"><a href="py3-ct.html#python"><i class="fa fa-check"></i><b>4.1</b> Python实战</a><ul>
<li class="chapter" data-level="4.1.1" data-path="Py3-pratcise-ct.html"><a href="Py3-pratcise-ct.html#id"><i class="fa fa-check"></i><b>4.1.1</b> ID转换</a></li>
<li class="chapter" data-level="4.1.2" data-path="Py3-pratcise-ct.html"><a href="Py3-pratcise-ct.html#section-4.1.2"><i class="fa fa-check"></i><b>4.1.2</b> 每条染色体基因数目统计</a></li>
<li class="chapter" data-level="4.1.3" data-path="Py3-pratcise-ct.html"><a href="Py3-pratcise-ct.html#section-4.1.3"><i class="fa fa-check"></i><b>4.1.3</b> 所有外显子总长度统计</a></li>
</ul></li>
<li class="chapter" data-level="4.2" data-path="py3-ct.html"><a href="py3-ct.html#python"><i class="fa fa-check"></i><b>4.2</b> Python小技巧</a></li>
</ul></li>
<li class="chapter" data-level="5" data-path="py3-pandas-ct.html"><a href="py3-pandas-ct.html"><i class="fa fa-check"></i><b>5</b> Pandas 学习教程</a><ul>
<li class="chapter" data-level="5.1" data-path="py3-pandas-ct.html"><a href="py3-pandas-ct.html#what-is-pandas"><i class="fa fa-check"></i><b>5.1</b> What is pandas</a></li>
<li class="chapter" data-level="5.2" data-path="py3-pandas-ct.html"><a href="py3-pandas-ct.html#pandas"><i class="fa fa-check"></i><b>5.2</b> Pandas读取文件</a><ul>
<li class="chapter" data-level="5.2.1" data-path="py3-pandas-ct.html"><a href="py3-pandas-ct.html#section-5.2.1"><i class="fa fa-check"></i><b>5.2.1</b> 获取目标文件</a></li>
<li class="chapter" data-level="5.2.2" data-path="py3-pandas-ct.html"><a href="py3-pandas-ct.html#section-5.2.2"><i class="fa fa-check"></i><b>5.2.2</b> 查看目标文件内容和格式</a></li>
<li class="chapter" data-level="5.2.3" data-path="py3-pandas-ct.html"><a href="py3-pandas-ct.html#section-5.2.3"><i class="fa fa-check"></i><b>5.2.3</b> 读取两列文件</a></li>
<li class="chapter" data-level="5.2.4" data-path="py3-pandas-ct.html"><a href="py3-pandas-ct.html#section-5.2.4"><i class="fa fa-check"></i><b>5.2.4</b> 数据表的索引</a></li>
<li class="chapter" data-level="5.2.5" data-path="py3-pandas-ct.html"><a href="py3-pandas-ct.html#section-5.2.5"><i class="fa fa-check"></i><b>5.2.5</b> 读取多列文件</a></li>
<li class="chapter" data-level="5.2.6" data-path="py3-pandas-ct.html"><a href="py3-pandas-ct.html#section-5.2.6"><i class="fa fa-check"></i><b>5.2.6</b> 选取多列数据</a></li>
<li class="chapter" data-level="5.2.7" data-path="py3-pandas-ct.html"><a href="py3-pandas-ct.html#section-5.2.7"><i class="fa fa-check"></i><b>5.2.7</b> 重命名列名字</a></li>
<li class="chapter" data-level="5.2.8" data-path="py3-pandas-ct.html"><a href="py3-pandas-ct.html#section-5.2.8"><i class="fa fa-check"></i><b>5.2.8</b> 合并矩阵</a></li>
<li class="chapter" data-level="5.2.9" data-path="py3-pandas-ct.html"><a href="py3-pandas-ct.html#section-5.2.9"><i class="fa fa-check"></i><b>5.2.9</b> 矩阵数据提取</a></li>
<li class="chapter" data-level="5.2.10" data-path="py3-pandas-ct.html"><a href="py3-pandas-ct.html#meta-data"><i class="fa fa-check"></i><b>5.2.10</b> 读取META data文件</a></li>
</ul></li>
<li class="chapter" data-level="5.3" data-path="py3-pandas-ct.html"><a href="py3-pandas-ct.html#pandas"><i class="fa fa-check"></i><b>5.3</b> Pandas写入文件</a><ul>
<li class="chapter" data-level="5.3.1" data-path="py3-pandas-ct.html"><a href="py3-pandas-ct.html#section-5.3.1"><i class="fa fa-check"></i><b>5.3.1</b> 写入文本文件</a></li>
</ul></li>
<li class="chapter" data-level="5.4" data-path="py3-pandas-ct.html"><a href="py3-pandas-ct.html#pandas"><i class="fa fa-check"></i><b>5.4</b> PANDAS矩阵的小应用</a></li>
<li class="chapter" data-level="5.5" data-path="py3-pandas-ct.html"><a href="py3-pandas-ct.html#seaborn"><i class="fa fa-check"></i><b>5.5</b> Seaborn绘图</a></li>
</ul></li>
<li class="chapter" data-level="6" data-path="numpy-scipy.html"><a href="numpy-scipy.html"><i class="fa fa-check"></i><b>6</b> Python科学计算</a><ul>
<li class="chapter" data-level="6.1" data-path="numpy-scipy.html"><a href="numpy-scipy.html#numpy"><i class="fa fa-check"></i><b>6.1</b> NumPy</a><ul>
<li class="chapter" data-level="6.1.1" data-path="numpy-scipy.html"><a href="numpy-scipy.html#numpy"><i class="fa fa-check"></i><b>6.1.1</b> NumPy数组</a></li>
<li class="chapter" data-level="6.1.2" data-path="numpy-scipy.html"><a href="numpy-scipy.html#numpy"><i class="fa fa-check"></i><b>6.1.2</b> 布尔语句和NumPy数组</a></li>
<li class="chapter" data-level="6.1.3" data-path="numpy-scipy.html"><a href="numpy-scipy.html#numpy"><i class="fa fa-check"></i><b>6.1.3</b> NumPy读写文件</a></li>
<li class="chapter" data-level="6.1.4" data-path="numpy-scipy.html"><a href="numpy-scipy.html#numpymath"><i class="fa fa-check"></i><b>6.1.4</b> NumPy的Math模块</a></li>
</ul></li>
<li class="chapter" data-level="6.2" data-path="numpy-scipy.html"><a href="numpy-scipy.html#scipy"><i class="fa fa-check"></i><b>6.2</b> SciPy</a><ul>
<li class="chapter" data-level="6.2.1" data-path="numpy-scipy.html"><a href="numpy-scipy.html#section-6.2.1"><i class="fa fa-check"></i><b>6.2.1</b> 最优化和最小化</a></li>
<li class="chapter" data-level="6.2.2" data-path="numpy-scipy.html"><a href="numpy-scipy.html#section-6.2.2"><i class="fa fa-check"></i><b>6.2.2</b> 插值</a></li>
</ul></li>
</ul></li>
<li class="chapter" data-level="7" data-path="Py3-test.html"><a href="Py3-test.html"><i class="fa fa-check"></i><b>7</b> 易生信Python培训练习和考核题目</a></li>
<li class="chapter" data-level="8" data-path="references.html"><a href="references.html"><i class="fa fa-check"></i><b>8</b> 生信教程文章集锦</a><ul>
<li class="chapter" data-level="8.1" data-path="references.html"><a href="references.html#section-8.1"><i class="fa fa-check"></i><b>8.1</b> 生信宝典</a><ul>
<li class="chapter" data-level="8.1.1" data-path="references.html"><a href="references.html#section-8.1.1"><i class="fa fa-check"></i><b>8.1.1</b> 系列教程</a></li>
<li class="chapter" data-level="8.1.2" data-path="references.html"><a href="references.html#ngs"><i class="fa fa-check"></i><b>8.1.2</b> NGS分析工具评估</a></li>
<li class="chapter" data-level="8.1.3" data-path="references.html"><a href="references.html#section-8.1.3"><i class="fa fa-check"></i><b>8.1.3</b> 宏基因组教程</a></li>
<li class="chapter" data-level="8.1.4" data-path="references.html"><a href="references.html#section-8.1.4"><i class="fa fa-check"></i><b>8.1.4</b> 系列宣传</a></li>
<li class="chapter" data-level="8.1.5" data-path="references.html"><a href="references.html#section-8.1.5"><i class="fa fa-check"></i><b>8.1.5</b> 生信生物知识</a></li>
<li class="chapter" data-level="8.1.6" data-path="references.html"><a href="references.html#section-8.1.6"><i class="fa fa-check"></i><b>8.1.6</b> 文献精读</a></li>
<li class="chapter" data-level="8.1.7" data-path="references.html"><a href="references.html#linux"><i class="fa fa-check"></i><b>8.1.7</b> Linux</a></li>
<li class="chapter" data-level="8.1.8" data-path="references.html"><a href="references.html#circos"><i class="fa fa-check"></i><b>8.1.8</b> CIRCOS系列</a></li>
<li class="chapter" data-level="8.1.9" data-path="references.html"><a href="references.html#r"><i class="fa fa-check"></i><b>8.1.9</b> R统计和作图</a></li>
<li class="chapter" data-level="8.1.10" data-path="references.html"><a href="references.html#section-8.1.10"><i class="fa fa-check"></i><b>8.1.10</b> 扩增子三步曲</a></li>
<li class="chapter" data-level="8.1.11" data-path="references.html"><a href="references.html#section-8.1.11"><i class="fa fa-check"></i><b>8.1.11</b> 宏基因组分析专题</a></li>
<li class="chapter" data-level="8.1.12" data-path="references.html"><a href="references.html#ngs"><i class="fa fa-check"></i><b>8.1.12</b> NGS基础</a></li>
<li class="chapter" data-level="8.1.13" data-path="references.html"><a href="references.html#section-8.1.13"><i class="fa fa-check"></i><b>8.1.13</b> 癌症数据库</a></li>
<li class="chapter" data-level="8.1.14" data-path="references.html"><a href="references.html#python-1"><i class="fa fa-check"></i><b>8.1.14</b> Python</a></li>
<li class="chapter" data-level="8.1.15" data-path="references.html"><a href="references.html#ngs"><i class="fa fa-check"></i><b>8.1.15</b> NGS软件</a></li>
<li class="chapter" data-level="8.1.16" data-path="references.html"><a href="references.html#cytoscape"><i class="fa fa-check"></i><b>8.1.16</b> Cytoscape网络图</a></li>
<li class="chapter" data-level="8.1.17" data-path="references.html"><a href="references.html#section-8.1.17"><i class="fa fa-check"></i><b>8.1.17</b> 分子对接</a></li>
<li class="chapter" data-level="8.1.18" data-path="references.html"><a href="references.html#section-8.1.18"><i class="fa fa-check"></i><b>8.1.18</b> 生信宝典之傻瓜式</a></li>
<li class="chapter" data-level="8.1.19" data-path="references.html"><a href="references.html#section-8.1.19"><i class="fa fa-check"></i><b>8.1.19</b> 生信人写程序</a></li>
<li class="chapter" data-level="8.1.20" data-path="references.html"><a href="references.html#section-8.1.20"><i class="fa fa-check"></i><b>8.1.20</b> 小技巧系列</a></li>
<li class="chapter" data-level="8.1.21" data-path="references.html"><a href="references.html#section-8.1.21"><i class="fa fa-check"></i><b>8.1.21</b> 招聘</a></li>
</ul></li>
<li class="chapter" data-level="8.2" data-path="references.html"><a href="references.html#section-8.2"><i class="fa fa-check"></i><b>8.2</b> 宏基因组</a><ul>
<li class="chapter" data-level="8.2.1" data-path="references.html"><a href="references.html#section-8.2.1"><i class="fa fa-check"></i><b>8.2.1</b> 精选文章推荐</a></li>
<li class="chapter" data-level="8.2.2" data-path="references.html"><a href="references.html#section-8.2.2"><i class="fa fa-check"></i><b>8.2.2</b> 培训、会议、征稿、招聘</a></li>
<li class="chapter" data-level="8.2.3" data-path="references.html"><a href="references.html#section-8.2.3"><i class="fa fa-check"></i><b>8.2.3</b> 科研经验</a></li>
<li class="chapter" data-level="8.2.4" data-path="references.html"><a href="references.html#section-8.2.4"><i class="fa fa-check"></i><b>8.2.4</b> 软件和数据库使用</a></li>
<li class="chapter" data-level="8.2.5" data-path="references.html"><a href="references.html#section-8.2.5"><i class="fa fa-check"></i><b>8.2.5</b> 扩增子学习三步曲</a></li>
<li class="chapter" data-level="8.2.6" data-path="py3-ct.html"><a href="py3-ct.html#-1"><i class="fa fa-check"></i><b>8.2.6</b> 宏基因组分析专题</a></li>
<li class="chapter" data-level="8.2.7" data-path="references.html"><a href="references.html#r"><i class="fa fa-check"></i><b>8.2.7</b> R统计绘图</a></li>
<li class="chapter" data-level="8.2.8" data-path="references.html"><a href="references.html#section-8.2.8"><i class="fa fa-check"></i><b>8.2.8</b> 实验设计与技术</a></li>
<li class="chapter" data-level="8.2.9" data-path="references.html"><a href="references.html#section-8.2.9"><i class="fa fa-check"></i><b>8.2.9</b> 基础知识</a></li>
<li class="chapter" data-level="8.2.10" data-path="references.html"><a href="references.html#section-8.2.10"><i class="fa fa-check"></i><b>8.2.10</b> 必读综述</a></li>
<li class="chapter" data-level="8.2.11" data-path="references.html"><a href="references.html#section-8.2.11"><i class="fa fa-check"></i><b>8.2.11</b> 高分文章套路解读</a></li>
<li class="chapter" data-level="8.2.12" data-path="pythonbasic.html"><a href="pythonbasic.html#-"><i class="fa fa-check"></i><b>8.2.12</b> 科普视频-寓教于乐</a></li>
<li class="chapter" data-level="8.2.13" data-path="references.html"><a href="references.html#section-8.2.13"><i class="fa fa-check"></i><b>8.2.13</b> 友军文章汇总推荐</a></li>
</ul></li>
</ul></li>
<li class="chapter" data-level="9" data-path="company-intro.html"><a href="company-intro.html"><i class="fa fa-check"></i><b>9</b> 公司简介</a></li>
<li class="divider"></li>
<li><a href="mailto:ct@ehbio.com" target="blank">ct@ehbio.com</a></li>

</ul>

      </nav>
    </div>

    <div class="book-body">
      <div class="body-inner">
        <div class="book-header" role="navigation">
          <h1>
            <i class="fa fa-circle-o-notch fa-spin"></i><a href="./">Python3学习教程</a>
          </h1>
        </div>

        <div class="page-wrapper" tabindex="-1" role="main">
          <div class="page-inner">

            <section class="normal" id="section-">
<div id="Py3_pratcise_ct" class="section level1">
<h1><span class="header-section-number">4</span> Python实战</h1>
<p>欢迎访问我们的视频课程 <a href="https://bioinfo.ke.qq.com" class="uri">https://bioinfo.ke.qq.com</a>。</p>
<div id="python" class="section level2">
<h2><span class="header-section-number">4.1</span> Python实战</h2>
<div id="id" class="section level3">
<h3><span class="header-section-number">4.1.1</span> ID转换</h3>
<p>不同数据库的名字互相转换，比如NCBI的ENTEZ ID转Gene symbol，ENSEMBL编号转ENTREZ ID。</p>
<p>ID转换一般需要2个文件，一个文件是要转换的ID，另外一个文件是ID的对应关系。</p>
<p>以我们的测试文件 (<strong>GRCh38.idmap</strong>)为例，第一列为<code>ENSEMBLE ID</code>，第二列为<code>Gene symbol</code>，第三列为<code>Entrez ID</code>。</p>
<pre><code>Gene ID Associated Gene Name    EntrezGene ID
ENSG00000252303 RNU6-280P       
ENSG00000281771 Y_RNA      
ENSG00000281876 RP11-399E6.4    101929901
ENSG00000281766 RYBP    23429
ENSG00000281518 FOXO6   100132074
ENSG00000281614 INPP5D  3635
ENSG00000280584 OBP2B   29989
ENSG00000281230 SERTAD4 56256
ENSG00000281917 SLC16A1 6566</code></pre>
<p>待转换的文件 (<strong>ensm.id</strong>)内容为，期望转换为<code>Gene symbol</code>。</p>
<pre><code>ENSG00000252303
ENSG00000281771
ENSG00000281256
ENSG00000283272
ENSG00000280864
ENSG00000280792
ENSG00000282878
ENSG00000283276
ENSG00000281822
ENSG00000281384
ENSG00000280505
ENSG00000281764
ENSG00000281316
ENSG00000280963
ENSG00000280775
ENSG00000281876
ENSG00000281766
ENSG00000281518
ENSG00000281614
ENSG00000280584
ENSG00000281230
ENSG00000281917</code></pre>
<p>我们怎么做呢？ 直观看起来也比较简单，一个个的去比较、匹配、提取就可以了。</p>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python">idD <span class="op">=</span> {}
<span class="cf">for</span> line <span class="op">in</span> <span class="bu">open</span>(<span class="st">&quot;data/GRCh38.idmap&quot;</span>):
    lineL <span class="op">=</span> line.strip().split(<span class="st">&quot;</span><span class="ch">\t</span><span class="st">&quot;</span>)
    ensm_id <span class="op">=</span> lineL[<span class="dv">0</span>]
    symbol  <span class="op">=</span> lineL[<span class="dv">1</span>]
    idD[ensm_id] <span class="op">=</span> symbol
<span class="co">#--------------------------------</span>
<span class="cf">for</span> line <span class="op">in</span> <span class="bu">open</span>(<span class="st">&quot;data/ensm.id&quot;</span>):
    ensm <span class="op">=</span> line.strip()
    <span class="bu">print</span>(ensm,idD[ensm],sep<span class="op">=</span><span class="st">&quot;: &quot;</span>)</code></pre></div>
<pre><code>ENSG00000252303: RNU6-280P
ENSG00000281771: Y_RNA
ENSG00000281256: RP11-222G7.2
ENSG00000283272: Clostridiales-1
ENSG00000280864: RP11-654C22.2
ENSG00000280792: RP11-315F22.1
ENSG00000282878: RP11-399E6.1
ENSG00000283276: ABBA01000934.1
ENSG00000281822: RNU1-62P
ENSG00000281384: AC093802.1
ENSG00000280505: RP11-654C22.1
ENSG00000281764: RP11-399E6.2
ENSG00000281316: DPPA2P2
ENSG00000280963: SERTAD4-AS1
ENSG00000280775: RNA5SP136
ENSG00000281876: RP11-399E6.4
ENSG00000281766: RYBP
ENSG00000281518: FOXO6
ENSG00000281614: INPP5D
ENSG00000280584: OBP2B
ENSG00000281230: SERTAD4
ENSG00000281917: SLC16A1</code></pre>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python"><span class="co"># 首先读入GRCh38.idmap文件</span>

<span class="co"># 定义3个变量，文件名，ensembl_id所在列(0-start, source_col), symbol所在列 (1,target_col)</span>
<span class="co"># 提前定义的好处是，修改起来会比较方便。</span>
map_file <span class="op">=</span> <span class="st">&quot;data/GRCh38.idmap&quot;</span>
source_col <span class="op">=</span> <span class="dv">0</span>
target_col <span class="op">=</span> <span class="dv">1</span>
aDict <span class="op">=</span> {}
<span class="cf">for</span> line <span class="op">in</span> <span class="bu">open</span>(map_file):
    lineL <span class="op">=</span> line.strip().split(<span class="st">&quot;</span><span class="ch">\t</span><span class="st">&quot;</span>)
    source <span class="op">=</span> lineL[source_col]
    target <span class="op">=</span> lineL[target_col]
    aDict[source] <span class="op">=</span> target

<span class="co"># 读入ensm.id文件</span>
<span class="co"># 边读边处理</span>

id_file <span class="op">=</span> <span class="st">&quot;data/ensm.id&quot;</span>
<span class="cf">for</span> line <span class="op">in</span> <span class="bu">open</span>(id_file):
    source_id <span class="op">=</span> line.strip()
    map_id <span class="op">=</span> aDict.get(source_id, source_id)
    <span class="bu">print</span>(map_id)

<span class="co"># 输出到文件</span>

<span class="cf">with</span> <span class="bu">open</span>(<span class="st">&quot;data/symbol.id&quot;</span>,<span class="st">&quot;w&quot;</span>) <span class="im">as</span> fh:
    id_file <span class="op">=</span> <span class="st">&quot;data/ensm.id&quot;</span>
    <span class="cf">for</span> line <span class="op">in</span> <span class="bu">open</span>(id_file):
        source_id <span class="op">=</span> line.strip()
        map_id <span class="op">=</span> aDict.get(source_id, source_id)
        <span class="bu">print</span>(map_id, <span class="bu">file</span><span class="op">=</span>fh)</code></pre></div>
<pre><code>RNU6-280P
Y_RNA
RP11-222G7.2
Clostridiales-1
RP11-654C22.2
RP11-315F22.1
RP11-399E6.1
ABBA01000934.1
RNU1-62P
AC093802.1
RP11-654C22.1
RP11-399E6.2
DPPA2P2
SERTAD4-AS1
RNA5SP136
RP11-399E6.4
RYBP
FOXO6
INPP5D
OBP2B
SERTAD4
SLC16A1</code></pre>
</div>
<div id="section-4.1.2" class="section level3">
<h3><span class="header-section-number">4.1.2</span> 每条染色体基因数目统计</h3>
<p>GTF文件存储基因的注释信息。</p>
<ol style="list-style-type: decimal">
<li>seqname - name of the chromosome or scaffold; chromosome names can be given with or without the ‘chr’ prefix. Important note: the seqname must be one used within Ensembl, i.e. a standard chromosome name or an Ensembl identifier such as a scaffold ID, without any additional content such as species or assembly. See the example GFF output below.</li>
<li>source - name of the program that generated this feature, or the data source (database or project name)</li>
<li>feature - feature type name, e.g. Gene, Variation, Similarity</li>
<li>start - Start position of the feature, with sequence numbering starting at 1.</li>
<li>end - End position of the feature, with sequence numbering starting at 1.</li>
<li>score - A floating point value.</li>
<li>strand - defined as + (forward) or - (reverse).</li>
<li>frame - One of ‘0’, ‘1’ or ‘2’. ‘0’ indicates that the first base of the feature is the first base of a codon, ‘1’ that the second base is the first base of a codon, and so on..</li>
<li>attribute - A semicolon-separated list of tag-value pairs, providing additional information about each feature.</li>
</ol>
<pre><code>chr1    HAVANA  gene    11869   14409   .   +   .   gene_id &quot;ENSG00000223972.5&quot;; gene_type &quot;transcribed_unprocessed_pseudogene&quot;; gene_status &quot;KNOWN&quot;; gene_name &quot;DDX11L1&quot;;  level 2; havana_gene &quot;OTTHUMG00000000961.2&quot;;
chr1    HAVANA  gene    14404   29570   .   -   .   gene_id &quot;ENSG00000227232.5&quot;; gene_type &quot;unprocessed_pseudogene&quot;; gene_status &quot;KNOWN&quot;; gene_name &quot;WASH7P&quot;;  level 2; havana_gene &quot;OTTHUMG00000000958.1&quot;;
chr1    ENSEMBL gene    17369   17436   .   -   .   gene_id &quot;ENSG00000278267.1&quot;; gene_type &quot;miRNA&quot;; gene_status &quot;KNOWN&quot;; gene_name &quot;MIR6859-3&quot;;  level 3;</code></pre>
<p>如果统计每条染色体基因数目，只需要读第一列和第三列就可以了。</p>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python">chr_gene_cntD <span class="op">=</span> {}
<span class="cf">for</span> line <span class="op">in</span> <span class="bu">open</span>(<span class="st">&quot;data/gencode.gene.gtf&quot;</span>):
    lineL <span class="op">=</span> line.strip().split(<span class="st">&quot;</span><span class="ch">\t</span><span class="st">&quot;</span>)
    chrName <span class="op">=</span> lineL[<span class="dv">0</span>]
    regionType <span class="op">=</span> lineL[<span class="dv">2</span>]
    <span class="cf">if</span> (regionType <span class="op">==</span> <span class="st">&quot;gene&quot;</span>):
        <span class="cf">if</span> chrName <span class="op">not</span> <span class="op">in</span> chr_gene_cntD:
            chr_gene_cntD[chrName] <span class="op">=</span> <span class="dv">1</span>
        <span class="cf">else</span>:
            chr_gene_cntD[chrName] <span class="op">+=</span> <span class="dv">1</span>
<span class="co">#---------------------------------</span>
<span class="cf">for</span> chrName, cnt <span class="op">in</span> <span class="bu">list</span>(chr_gene_cntD.items()):
    <span class="bu">print</span>(chrName,<span class="st">&quot;has&quot;</span>,cnt,<span class="st">&#39;genes.&#39;</span>)</code></pre></div>
<pre><code>chr1 has 5397 genes.
chr2 has 4150 genes.
chr3 has 3163 genes.
chr4 has 2633 genes.
chr5 has 2993 genes.
chr6 has 3001 genes.
chr7 has 2980 genes.
chr8 has 2444 genes.
chr9 has 2350 genes.
chr10 has 2306 genes.
chr11 has 3381 genes.
chr12 has 3047 genes.
chr13 has 1383 genes.
chr14 has 2289 genes.
chr15 has 2247 genes.
chr16 has 2597 genes.
chr17 has 3111 genes.
chr18 has 1206 genes.
chr19 has 2997 genes.
chr20 has 1436 genes.
chr21 has 880 genes.
chr22 has 1385 genes.
chrX has 2476 genes.
chrY has 594 genes.
chrM has 37 genes.</code></pre>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python">gtf_file <span class="op">=</span> <span class="st">&quot;data/gencode.gene.gtf&quot;</span>

<span class="co"># 一般字典命名，会在行尾加一个大写的D，作为类型代表</span>
chr_gene_cntD <span class="op">=</span> {}

<span class="cf">for</span> line <span class="op">in</span> <span class="bu">open</span>(gtf_file):
    <span class="co"># 每行按TAB键分成4份</span>
    lineL <span class="op">=</span> line.split(<span class="st">&#39;</span><span class="ch">\t</span><span class="st">&#39;</span>,<span class="dv">3</span>)
    chr_name <span class="op">=</span> lineL[<span class="dv">0</span>]
    feature  <span class="op">=</span> lineL[<span class="dv">2</span>]
    <span class="cf">if</span> feature <span class="op">==</span> <span class="st">&quot;gene&quot;</span>:
        <span class="co"># 如果chr_name已经出现过，则获取其值，然后加1</span>
        <span class="co"># 若没出现过，获取0，加1，表示第一次出现</span>
        chr_gene_cntD[chr_name] <span class="op">=</span> chr_gene_cntD.get(chr_name,<span class="dv">0</span>) <span class="op">+</span> <span class="dv">1</span>
<span class="co">#--------------------------------------</span>
<span class="cf">for</span> chr_name, count <span class="op">in</span> chr_gene_cntD.items():
    <span class="bu">print</span>(chr_name, count)</code></pre></div>
<pre><code>chr1 5397
chr2 4150
chr3 3163
chr4 2633
chr5 2993
chr6 3001
chr7 2980
chr8 2444
chr9 2350
chr10 2306
chr11 3381
chr12 3047
chr13 1383
chr14 2289
chr15 2247
chr16 2597
chr17 3111
chr18 1206
chr19 2997
chr20 1436
chr21 880
chr22 1385
chrX 2476
chrY 594
chrM 37</code></pre>
</div>
<div id="section-4.1.3" class="section level3">
<h3><span class="header-section-number">4.1.3</span> 所有外显子总长度统计</h3>
<p>bed12格式 (前3列必须，其它可选)</p>
<ol style="list-style-type: decimal">
<li>chrom - The name of the chromosome (e.g. chr3, chrY, chr2_random) or scaffold (e.g. scaffold10671).</li>
<li>chromStart - The starting position of the feature in the chromosome or scaffold. The first base in a chromosome is numbered 0.</li>
<li>chromEnd - The ending position of the feature in the chromosome or scaffold. The chromEnd base is not included in the display of the feature.</li>
<li>name - Defines the name of the BED line. This label is displayed to the left of the BED line in the Genome Browser window when the track is open to full display mode or directly to the left of the item in pack mode.</li>
<li><p>score - A score between 0 and 1000. If the track line useScore attribute is set to 1 for this annotation data set, the score value will determine the level of gray in which this feature is displayed (higher numbers = darker gray). This table shows the Genome Browser’s translation of BED score values into shades of gray:</p></li>
<li>thickStart - The starting position at which the feature is drawn thickly (for example, the start codon in gene displays). When there is no thick part, thickStart and thickEnd are usually set to the chromStart position.</li>
<li>thickEnd - The ending position at which the feature is drawn thickly (for example the stop codon in gene displays).</li>
<li>itemRgb - An RGB value of the form R,G,B (e.g. 255,0,0). If the track line itemRgb attribute is set to “On”, this RBG value will determine the display color of the data contained in this BED line. NOTE: It is recommended that a simple color scheme (eight colors or less) be used with this attribute to avoid overwhelming the color resources of the Genome Browser and your Internet browser.</li>
<li>blockCount - The number of blocks (exons) in the BED line.</li>
<li>blockSizes - A comma-separated list of the block sizes. The number of items in this list should correspond to blockCount.</li>
<li><p>blockStarts - A comma-separated list of block starts. All of the blockStart positions should be calculated relative to chromStart. The number of items in this list should correspond to blockCount.</p></li>
</ol>
<p>基因注释bed12格式</p>
<pre><code>chr1    11868   14409   ENST00000456328.2   0   +   14409   14409   0   3   359,109,1189,   0,744,1352,
chr1    12009   13670   ENST00000450305.2   0   +   13670   13670   0   6   48,49,85,78,154,218,    0,169,603,965,1211,1443,
chr1    17368   17436   ENST00000619216.1   0   -   17436   17436   0   1   68, 0,
chr1    29553   31097   ENST00000473358.1   0   +   31097   31097   0   3   486,104,122,    0,1010,1422,
chr1    30266   31109   ENST00000469289.1   0   +   31109   31109   0   2   401,134,    0,709,
chr1    30365   30503   ENST00000607096.1   0   +   30503   30503   0   1   138,    0,</code></pre>
<p>第十列是外显子的大小，所有我们只需要把他们加在一起就好了。</p>
<p><strong>Note</strong>：实际计算时需要考虑不同转录本之间存在重叠，需要对bed文件预处理，只保留唯一的外显子位置，然后再加和。</p>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python">exonSizeSum <span class="op">=</span> <span class="dv">0</span>
<span class="cf">for</span> line <span class="op">in</span> <span class="bu">open</span>(<span class="st">&quot;data/gencode.gene.bed12&quot;</span>):
    lineL <span class="op">=</span> line.split()
    exonSize <span class="op">=</span> lineL[<span class="dv">10</span>]
    exonSizeL <span class="op">=</span> exonSize.strip(<span class="st">&#39;,&#39;</span>).split(<span class="st">&#39;,&#39;</span>)
    <span class="cf">for</span> i <span class="op">in</span> exonSizeL:
        exonSizeSum <span class="op">+=</span> <span class="bu">int</span>(i)
<span class="bu">print</span>(exonSizeSum)</code></pre></div>
<pre><code>291762517</code></pre>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python">bed12 <span class="op">=</span> <span class="st">&quot;data/gencode.gene.bed12&quot;</span>

total_exon_sum <span class="op">=</span> <span class="dv">0</span>

<span class="cf">for</span> line <span class="op">in</span> <span class="bu">open</span>(bed12):
    <span class="co"># 从右侧分割为3个元素的列表</span>
    <span class="co"># 关注的元素在第二位，索引为1</span>
    <span class="co"># 去掉末尾的逗号</span>
    lineL <span class="op">=</span> line.rsplit(<span class="st">&quot;</span><span class="ch">\t</span><span class="st">&quot;</span>,<span class="dv">2</span>)
    exonSize <span class="op">=</span> lineL[<span class="dv">1</span>].strip(<span class="st">&#39;,&#39;</span>)
    exonSizeL <span class="op">=</span> [<span class="bu">int</span>(i) <span class="cf">for</span> i <span class="op">in</span> exonSize.split(<span class="st">&#39;,&#39;</span>)]
    exonSum <span class="op">=</span> <span class="bu">sum</span>(exonSizeL)
    
    total_exon_sum <span class="op">+=</span> exonSum
<span class="bu">print</span>(<span class="st">&quot;Total exon size is&quot;</span>, total_exon_sum, <span class="st">&quot;nt.&quot;</span>)

<span class="co"># 更清晰展示数字大小</span>
<span class="bu">print</span>(<span class="st">&quot;Total exon size is&quot;</span>, <span class="st">&quot;{:,}&quot;</span>.<span class="bu">format</span>(total_exon_sum), <span class="st">&quot;nt.&quot;</span>)</code></pre></div>
<pre><code>Total exon size is 291762517 nt.
Total exon size is 291,762,517 nt.</code></pre>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python"><span class="bu">print</span>(<span class="st">&quot;</span><span class="sc">{a[a]}</span><span class="st">&quot;</span>.<span class="bu">format</span>(a<span class="op">=</span>{<span class="st">&#39;a&#39;</span>:<span class="st">&quot;ehbio&quot;</span>}))</code></pre></div>
<pre><code>ehbio</code></pre>
</div>
</div>
<div id="python" class="section level2">
<h2><span class="header-section-number">4.2</span> Python小技巧</h2>
<p>链似比较</p>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python">x <span class="op">=</span> <span class="dv">5</span>
<span class="bu">print</span>(<span class="st">&quot;1 &lt; x &lt; 10 is&quot;</span>, <span class="dv">1</span> <span class="op">&lt;</span> x <span class="op">&lt;</span> <span class="dv">10</span>)
<span class="bu">print</span>(<span class="st">&quot;10 &gt; x &lt;= 9&quot;</span>, <span class="dv">10</span> <span class="op">&gt;</span> x <span class="op">&lt;=</span> <span class="dv">9</span>)</code></pre></div>
<pre><code>1 &lt; x &lt; 10 is True
10 &gt; x &lt;= 9 True</code></pre>
<p>解释正则表达式</p>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python"><span class="im">import</span> re
re.<span class="bu">compile</span>(<span class="st">&quot;^[a-z]*$&quot;</span>, re.DEBUG)</code></pre></div>
<pre><code>AT AT_BEGINNING
MAX_REPEAT 0 MAXREPEAT
  IN
    RANGE (97, 122)
AT AT_END





re.compile(r&#39;^[a-z]*$&#39;, re.UNICODE|re.DEBUG)</code></pre>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python">re.<span class="bu">compile</span>(<span class="st">&quot;^[a-z][0-9]+$&quot;</span>, re.DEBUG)</code></pre></div>
<pre><code>AT AT_BEGINNING
IN
  RANGE (97, 122)
MAX_REPEAT 1 MAXREPEAT
  IN
    RANGE (48, 57)
AT AT_END





re.compile(r&#39;^[a-z][0-9]+$&#39;, re.UNICODE|re.DEBUG)</code></pre>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python">re.<span class="bu">compile</span>(<span class="st">&quot;^[a-z]([0-9]+)$&quot;</span>, re.DEBUG)</code></pre></div>
<pre><code>AT AT_BEGINNING
IN
  RANGE (97, 122)
SUBPATTERN 1 0 0
  MAX_REPEAT 1 MAXREPEAT
    IN
      RANGE (48, 57)
AT AT_END





re.compile(r&#39;^[a-z]([0-9]+)$&#39;, re.UNICODE|re.DEBUG)</code></pre>
<p>enumerate (不再使用len)</p>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python">a <span class="op">=</span> [<span class="st">&#39;s&#39;</span>,<span class="st">&#39;x&#39;</span>,<span class="st">&#39;b&#39;</span>,<span class="st">&#39;d&#39;</span>]

<span class="co"># Preferred way</span>
<span class="cf">for</span> index, item <span class="op">in</span> <span class="bu">enumerate</span>(a):
    <span class="bu">print</span>(index,item)

<span class="bu">print</span>(<span class="st">&quot;</span><span class="ch">\n</span><span class="st">&quot;</span>)

<span class="co"># Old way</span>
<span class="cf">for</span> i <span class="op">in</span> <span class="bu">range</span>(<span class="bu">len</span>(a)):
    <span class="bu">print</span>(i,a[i])
    </code></pre></div>
<pre><code>0 s
1 x
2 b
3 d


0 s
1 x
2 b
3 d</code></pre>
<p>列表解析、字典解析、元组解析</p>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python"><span class="co"># 获得系列坐标点</span>
a <span class="op">=</span> ((i,j) <span class="cf">for</span> i <span class="op">in</span> <span class="bu">range</span>(<span class="dv">3</span>) <span class="cf">for</span> j <span class="op">in</span> <span class="bu">range</span>(<span class="dv">2</span>))
a</code></pre></div>
<pre><code>&lt;generator object &lt;genexpr&gt; at 0x7fa8d04d7fc0&gt;</code></pre>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python"><span class="cf">for</span> i <span class="op">in</span> a:
    <span class="bu">print</span>(i)</code></pre></div>
<pre><code>(0, 0)
(0, 1)
(1, 0)
(1, 1)
(2, 0)
(2, 1)</code></pre>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python">str1 <span class="op">=</span> <span class="st">&quot;I love sheng xin bao dian&quot;</span>
<span class="bu">print</span>([i <span class="cf">for</span> i <span class="op">in</span> str1.split() <span class="cf">if</span> i.endswith(<span class="st">&#39;n&#39;</span>)])</code></pre></div>
<pre><code>[&#39;xin&#39;, &#39;dian&#39;]</code></pre>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python">a <span class="op">=</span> {i:i<span class="op">*</span><span class="dv">2</span> <span class="cf">for</span> i <span class="op">in</span> <span class="bu">range</span>(<span class="dv">5</span>)}
a</code></pre></div>
<pre><code>{0: 0, 1: 2, 2: 4, 3: 6, 4: 8}</code></pre>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python">[(x, y) <span class="cf">for</span> x <span class="op">in</span> <span class="bu">range</span>(<span class="dv">4</span>) <span class="cf">if</span> x <span class="op">%</span> <span class="dv">2</span> <span class="op">==</span> <span class="dv">1</span> <span class="cf">for</span> y <span class="op">in</span> <span class="bu">range</span>(<span class="dv">4</span>)]</code></pre></div>
<pre><code>[(1, 0), (1, 1), (1, 2), (1, 3), (3, 0), (3, 1), (3, 2), (3, 3)]</code></pre>
<p>列表索引，反序</p>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python">a <span class="op">=</span> [<span class="dv">1</span>,<span class="dv">2</span>,<span class="dv">3</span>,<span class="dv">4</span>,<span class="dv">5</span>]
a[::<span class="dv">2</span>]</code></pre></div>
<pre><code>[1, 3, 5]</code></pre>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python">a[::<span class="op">-</span><span class="dv">1</span>]</code></pre></div>
<pre><code>[5, 4, 3, 2, 1]</code></pre>
<p>for..else;若<code>for</code>循环中未执行<code>break</code>,则<code>else</code>会被执行</p>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python">found <span class="op">=</span> <span class="va">False</span>
<span class="cf">for</span> i <span class="op">in</span> <span class="bu">range</span>(<span class="dv">2</span>,<span class="dv">5</span>):
    <span class="cf">if</span> i <span class="op">==</span> <span class="dv">1</span>:
        found <span class="op">=</span> <span class="va">True</span>
        <span class="cf">break</span>
<span class="cf">if</span> <span class="op">not</span> found: 
    <span class="bu">print</span>(<span class="st">&quot;i was never 1&quot;</span>)</code></pre></div>
<pre><code>i was never 1</code></pre>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python"><span class="cf">for</span> i <span class="op">in</span> <span class="bu">range</span>(<span class="dv">2</span>,<span class="dv">5</span>):
    <span class="cf">if</span> i <span class="op">==</span> <span class="dv">1</span>:
        <span class="cf">break</span>
<span class="cf">else</span>:
    <span class="bu">print</span>(<span class="st">&quot;i was never 1&quot;</span>)</code></pre></div>
<pre><code>i was never 1</code></pre>
<p>原位替换</p>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python">a <span class="op">=</span> <span class="dv">5</span>
b <span class="op">=</span> <span class="dv">6</span>
c <span class="op">=</span> <span class="dv">7</span>
a, b <span class="op">=</span> b,a
<span class="bu">print</span>(<span class="st">&quot;a is&quot;</span>,a)
<span class="bu">print</span>(<span class="st">&quot;b is&quot;</span>,b)</code></pre></div>
<pre><code>a is 6
b is 5</code></pre>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python">a, (b,c) <span class="op">=</span> c, (a,b)
<span class="bu">print</span>(<span class="st">&quot;a is&quot;</span>,a)
<span class="bu">print</span>(<span class="st">&quot;b is&quot;</span>,b)
<span class="bu">print</span>(<span class="st">&quot;c is&quot;</span>,c)</code></pre></div>
<pre><code>a is 7
b is 6
c is 5</code></pre>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python">first, <span class="op">*</span>middle_all, last <span class="op">=</span> (<span class="dv">1</span>,<span class="dv">2</span>,<span class="dv">3</span>,<span class="dv">4</span>,<span class="dv">5</span>,<span class="dv">6</span>)
middle_all</code></pre></div>
<pre><code>[2, 3, 4, 5]</code></pre>
<p>集合操作</p>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python">a <span class="op">=</span> <span class="bu">set</span>([<span class="st">&quot;sheng&quot;</span>, <span class="st">&quot;xin&quot;</span>, <span class="st">&quot;bao&quot;</span>,<span class="st">&quot;dian&quot;</span>,<span class="st">&quot;best&quot;</span>,<span class="st">&quot;tutotials&quot;</span>])
b <span class="op">=</span> <span class="bu">set</span>([<span class="st">&quot;hong&quot;</span>, <span class="st">&quot;ji&quot;</span>, <span class="st">&quot;yin&quot;</span>,<span class="st">&quot;zu&quot;</span>,<span class="st">&quot;best&quot;</span>,<span class="st">&quot;tutotials&quot;</span>])
a <span class="op">|</span> b  <span class="co"># union</span></code></pre></div>
<pre><code>{&#39;bao&#39;, &#39;best&#39;, &#39;dian&#39;, &#39;hong&#39;, &#39;ji&#39;, &#39;sheng&#39;, &#39;tutotials&#39;, &#39;xin&#39;, &#39;yin&#39;, &#39;zu&#39;}</code></pre>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python">a <span class="op">&amp;</span> b <span class="co"># intersection</span></code></pre></div>
<pre><code>{&#39;best&#39;, &#39;tutotials&#39;}</code></pre>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python">a <span class="op">^</span> b <span class="co"># Symmetric Difference</span></code></pre></div>
<pre><code>{&#39;bao&#39;, &#39;dian&#39;, &#39;hong&#39;, &#39;ji&#39;, &#39;sheng&#39;, &#39;xin&#39;, &#39;yin&#39;, &#39;zu&#39;}</code></pre>
<p>Negative round</p>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python"><span class="bu">print</span>(<span class="st">&quot;round整数:&quot;</span>,<span class="bu">str</span>(<span class="bu">round</span>(<span class="fl">1234.5678</span>, <span class="op">-</span><span class="dv">2</span>)))

<span class="bu">print</span>(<span class="st">&quot;round小数:&quot;</span>,<span class="bu">str</span>(<span class="bu">round</span>(<span class="fl">1234.5678</span>, <span class="dv">2</span>)))</code></pre></div>
<pre><code>round整数: 1200.0
round小数: 1234.57</code></pre>
<p>多行字符串的嵌套</p>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python"><span class="co"># \可以，但是第二行需要起头</span>
system_command <span class="op">=</span> <span class="st">&quot;s-plot pheatmap -f matrix \</span>
<span class="st">-t heatmap -a TRUE&quot;</span>
<span class="bu">print</span>(system_command)</code></pre></div>
<pre><code>s-plot pheatmap -f matrix -t heatmap -a TRUE</code></pre>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python"><span class="co"># 字符串中包含换行符</span>
<span class="co"># 切第二行要起头，不然会有较多空格</span>
system_command <span class="op">=</span> <span class="st">&quot;&quot;&quot;s-plot pheatmap -f matrix</span>
<span class="st">-t heatmap -a TRUE&quot;&quot;&quot;</span> 
<span class="bu">print</span>(system_command)
<span class="bu">print</span>(system_command.replace(<span class="st">&#39;</span><span class="ch">\n</span><span class="st">&#39;</span>, <span class="st">&#39; &#39;</span>))</code></pre></div>
<pre><code>s-plot pheatmap -f matrix 
-t heatmap -a TRUE
s-plot pheatmap -f matrix  -t heatmap -a TRUE</code></pre>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python"><span class="co"># 类元组的写法，既可以跨行，又可以自由格式</span>
<span class="co"># 需要注意2点</span>
   <span class="co"># 类元组，无逗号</span>
   <span class="co"># 字符串连接时不会自动加空格，空格需要保存在字符串里面 </span>
system_command <span class="op">=</span> (<span class="st">&quot;s-plot pheatmap -f matrix &quot;</span>
                  <span class="co">&quot;-t heatmap -a TRUE&quot;</span>)
<span class="bu">print</span>(system_command)</code></pre></div>
<pre><code>s-plot pheatmap -f matrix -t heatmap -a TRUE</code></pre>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python"><span class="co"># 多一步join；</span>
system_command <span class="op">=</span> [<span class="st">&quot;s-plot pheatmap -f matrix&quot;</span>,
                  <span class="co">&quot;-t heatmap -a TRUE&quot;</span>]
<span class="bu">print</span>(<span class="st">&#39; &#39;</span>.join(system_command))</code></pre></div>
<pre><code>s-plot pheatmap -f matrix -t heatmap -a TRUE</code></pre>
<p>矩阵转置</p>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python">a <span class="op">=</span> [(<span class="dv">1</span>,<span class="dv">2</span>), (<span class="dv">3</span>,<span class="dv">4</span>), (<span class="dv">5</span>,<span class="dv">6</span>)]
b <span class="op">=</span> <span class="bu">zip</span>(<span class="op">*</span>a)
<span class="cf">for</span> i <span class="op">in</span> b:
    <span class="bu">print</span>(i)</code></pre></div>
<pre><code>(1, 3, 5)
(2, 4, 6)</code></pre>
<p>zip转换两个列表为字典</p>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python">keyL <span class="op">=</span> [<span class="dv">1</span>,<span class="dv">2</span>,<span class="dv">3</span>]
valueL <span class="op">=</span> [<span class="st">&#39;a&#39;</span>,<span class="st">&#39;b&#39;</span>,<span class="st">&#39;v&#39;</span>]
<span class="cf">for</span> i ,j <span class="op">in</span> <span class="bu">zip</span>(keyL, valueL):
    <span class="bu">print</span>(i,j)</code></pre></div>
<pre><code>1 a
2 b
3 v</code></pre>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python"><span class="im">import</span> pprint
pprint.pprint(<span class="bu">dict</span>(<span class="bu">zip</span>(keyL, valueL)))</code></pre></div>
<pre><code>{1: &#39;a&#39;, 2: &#39;b&#39;, 3: &#39;v&#39;}</code></pre>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python"><span class="bu">dict</span>([(i,j) <span class="cf">for</span> i ,j <span class="op">in</span> <span class="bu">zip</span>(keyL, valueL)])</code></pre></div>
<pre><code>{1: &#39;a&#39;, 2: &#39;b&#39;, 3: &#39;v&#39;}</code></pre>
<p>重复</p>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python"><span class="co">&#39;xyz&#39;</span> <span class="op">*</span> <span class="dv">3</span></code></pre></div>
<pre><code>&#39;xyzxyzxyz&#39;</code></pre>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python"><span class="dv">3</span> <span class="op">*</span> <span class="st">&#39;xyz&#39;</span></code></pre></div>
<pre><code>&#39;xyzxyzxyz&#39;</code></pre>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python">[<span class="dv">1</span>,<span class="dv">2</span>] <span class="op">*</span> <span class="dv">3</span></code></pre></div>
<pre><code>[1, 2, 1, 2, 1, 2]</code></pre>
<p>启动网络服务器，用于文件预览或传输</p>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python"><span class="co"># run in commang line</span>
<span class="co"># python -m http.server 8000</span>
Serving HTTP on <span class="fl">0.0.0.0</span> port <span class="dv">8000</span> (http:<span class="op">//</span><span class="fl">0.0.0.0</span>:<span class="dv">8000</span><span class="op">/</span>) ...</code></pre></div>
<p>sum的另一用法，二维数组秒变1维</p>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python">aList <span class="op">=</span> [[<span class="dv">1</span>, <span class="dv">2</span>, <span class="dv">3</span>], [<span class="dv">4</span>, <span class="dv">5</span>], [<span class="dv">6</span>], [<span class="dv">7</span>, <span class="dv">8</span>, <span class="dv">9</span>]]
<span class="bu">sum</span>(aList, [])</code></pre></div>
<pre><code>[1, 2, 3, 4, 5, 6, 7, 8, 9]</code></pre>
<p>打开一个关于Python漫画的网站</p>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python"><span class="co">#import antigravity</span></code></pre></div>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python"><span class="bu">print</span>(<span class="st">&#39;</span><span class="ch">\n</span><span class="st">&#39;</span>.join([<span class="st">&#39;&#39;</span>.join([(<span class="st">&#39;Love&#39;</span>[(x<span class="op">-</span>y) <span class="op">%</span> <span class="bu">len</span>(<span class="st">&#39;Love&#39;</span>)] <span class="cf">if</span> ((x<span class="op">*</span><span class="fl">0.05</span>)<span class="op">**</span><span class="dv">2</span><span class="op">+</span>(y<span class="op">*</span><span class="fl">0.1</span>)<span class="op">**</span><span class="dv">2-1</span>)<span class="op">**</span><span class="dv">3</span><span class="op">-</span>(x<span class="op">*</span><span class="fl">0.05</span>)<span class="op">**</span><span class="dv">2</span><span class="op">*</span>(y<span class="op">*</span><span class="fl">0.1</span>)<span class="op">**</span><span class="dv">3</span> <span class="op">&lt;=</span> <span class="dv">0</span> <span class="cf">else</span> <span class="st">&#39; &#39;</span>) <span class="cf">for</span> x <span class="op">in</span> <span class="bu">range</span>(<span class="op">-</span><span class="dv">30</span>, <span class="dv">30</span>)]) <span class="cf">for</span> y <span class="op">in</span> <span class="bu">range</span>(<span class="dv">30</span>, <span class="op">-</span><span class="dv">30</span>, <span class="op">-</span><span class="dv">1</span>)]))</code></pre></div>
<pre><code>                veLoveLov           veLoveLov               
            eLoveLoveLoveLove   eLoveLoveLoveLove           
          veLoveLoveLoveLoveLoveLoveLoveLoveLoveLov         
         veLoveLoveLoveLoveLoveLoveLoveLoveLoveLoveL        
        veLoveLoveLoveLoveLoveLoveLoveLoveLoveLoveLov       
        eLoveLoveLoveLoveLoveLoveLoveLoveLoveLoveLove       
        LoveLoveLoveLoveLoveLoveLoveLoveLoveLoveLoveL       
        oveLoveLoveLoveLoveLoveLoveLoveLoveLoveLoveLo       
        veLoveLoveLoveLoveLoveLoveLoveLoveLoveLoveLov       
        eLoveLoveLoveLoveLoveLoveLoveLoveLoveLoveLove       
         oveLoveLoveLoveLoveLoveLoveLoveLoveLoveLove        
          eLoveLoveLoveLoveLoveLoveLoveLoveLoveLove         
          LoveLoveLoveLoveLoveLoveLoveLoveLoveLoveL         
            eLoveLoveLoveLoveLoveLoveLoveLoveLove           
             oveLoveLoveLoveLoveLoveLoveLoveLove            
              eLoveLoveLoveLoveLoveLoveLoveLove             
                veLoveLoveLoveLoveLoveLoveLov               
                  oveLoveLoveLoveLoveLoveLo                 
                    LoveLoveLoveLoveLoveL                   
                       LoveLoveLoveLov                      
                          LoveLoveL                         
                             Lov                            
                              v                             
                                                            
                                                            
                                                            
                                                            
                                                            
                                                            
                                                            
                                                            
                                                            
                                                            
                                                            
                                                            
                                                            
                                                            
                                                            
                                                            
                                                            
                                                            
                                                            </code></pre>
<p>曼德勃罗集合</p>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python"><span class="bu">print</span>(<span class="st">&#39;</span><span class="ch">\n</span><span class="st">&#39;</span>.join([<span class="st">&#39;&#39;</span>.join([<span class="st">&#39;*&#39;</span><span class="cf">if</span> <span class="bu">abs</span>((<span class="kw">lambda</span> a:<span class="kw">lambda</span> z,c,n:a(a,z,c,n))(<span class="kw">lambda</span> s,z,c,n:z <span class="cf">if</span> n<span class="op">==</span>0else s(s,z<span class="op">*</span>z<span class="op">+</span>c,c,n<span class="dv">-1</span>))(<span class="dv">0</span>,<span class="fl">0.02</span><span class="op">*</span>x<span class="fl">+0.</span>05j<span class="op">*</span>y,<span class="dv">40</span>))<span class="op">&lt;</span><span class="dv">2</span> <span class="cf">else</span><span class="st">&#39; &#39;</span><span class="cf">for</span> x <span class="op">in</span> <span class="bu">range</span>(<span class="op">-</span><span class="dv">80</span>,<span class="dv">20</span>)])<span class="cf">for</span> y <span class="op">in</span> <span class="bu">range</span>(<span class="op">-</span><span class="dv">20</span>,<span class="dv">20</span>)]))</code></pre></div>
<pre><code>                                                                                *                   
                                                                                                    
                                                                                                    
                                                                          **                        
                                                                     ***********                    
                                                                    ************                    
                                                                      *********                     
                                                            *    * ************  * *                
                                                    ****** * *************************** *          
                                                    *************************************** *****   
                                                     *******************************************    
                                               *** ******************************************** *   
                                                **************************************************  
                                             *******************************************************
                      *        *              ***************************************************** 
                     **** ******* *          *******************************************************
                      *****************     ******************************************************* 
                   *********************** *********************************************************
                   *********************** ******************************************************** 
             **** ********************************************************************************  
*********************************************************************************************       
             **** ********************************************************************************  
                   *********************** ******************************************************** 
                   *********************** *********************************************************
                      *****************     ******************************************************* 
                     **** ******* *          *******************************************************
                      *        *              ***************************************************** 
                                             *******************************************************
                                                **************************************************  
                                               *** ******************************************** *   
                                                     *******************************************    
                                                    *************************************** *****   
                                                    ****** * *************************** *          
                                                            *    * ************  * *                
                                                                      *********                     
                                                                    ************                    
                                                                     ***********                    
                                                                          **                        
                                                                                                    
                                                                                                    </code></pre>
<p>python打印九九乘法表</p>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python"><span class="bu">print</span>(<span class="st">&#39;</span><span class="ch">\n</span><span class="st">&#39;</span>.join([<span class="st">&#39; &#39;</span>.join([<span class="st">&#39;</span><span class="sc">%s</span><span class="st">*</span><span class="sc">%s</span><span class="st">=</span><span class="sc">%-2s</span><span class="st">&#39;</span> <span class="op">%</span> (y,x,x<span class="op">*</span>y) <span class="cf">for</span> y <span class="op">in</span> <span class="bu">range</span>(<span class="dv">1</span>,x<span class="dv">+1</span>)]) <span class="cf">for</span> x <span class="op">in</span> <span class="bu">range</span>(<span class="dv">1</span>,<span class="dv">10</span>)]))</code></pre></div>
<pre><code>1*1=1 
1*2=2  2*2=4 
1*3=3  2*3=6  3*3=9 
1*4=4  2*4=8  3*4=12 4*4=16
1*5=5  2*5=10 3*5=15 4*5=20 5*5=25
1*6=6  2*6=12 3*6=18 4*6=24 5*6=30 6*6=36
1*7=7  2*7=14 3*7=21 4*7=28 5*7=35 6*7=42 7*7=49
1*8=8  2*8=16 3*8=24 4*8=32 5*8=40 6*8=48 7*8=56 8*8=64
1*9=9  2*9=18 3*9=27 4*9=36 5*9=45 6*9=54 7*9=63 8*9=72 9*9=81</code></pre>
<p><code>map</code>, <code>filter</code></p>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python"><span class="co"># 过滤大于4的元素</span>
a <span class="op">=</span> [<span class="dv">3</span>,<span class="dv">4</span>,<span class="dv">5</span>]
[i <span class="cf">for</span> i <span class="op">in</span> a <span class="cf">if</span> i<span class="op">&lt;</span><span class="dv">4</span>]</code></pre></div>
<pre><code>[3]</code></pre>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python"><span class="bu">list</span>(<span class="bu">filter</span>(<span class="kw">lambda</span> x: x<span class="op">&lt;</span><span class="dv">4</span>, a))</code></pre></div>
<pre><code>[5]</code></pre>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python"><span class="co"># 每个元素加2</span>
[i<span class="dv">+2</span> <span class="cf">for</span> i <span class="op">in</span> a]</code></pre></div>
<pre><code>[5, 6, 7]</code></pre>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python"><span class="bu">map</span>(<span class="kw">lambda</span> x: x<span class="dv">+2</span>, a)</code></pre></div>
<pre><code>&lt;map at 0x7fa8d0502358&gt;</code></pre>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python"><span class="bu">list</span>(<span class="bu">map</span>(<span class="kw">lambda</span> x: x<span class="dv">+2</span>, a))</code></pre></div>
<pre><code>[5, 6, 7]</code></pre>
<p>算2的1000次方的各位数之和</p>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python"><span class="bu">sum</span>(<span class="bu">map</span>(<span class="bu">int</span>, <span class="bu">str</span>(<span class="dv">2</span><span class="op">**</span><span class="dv">1000</span>)))</code></pre></div>
<pre><code>1366</code></pre>

</div>
</div>
            </section>

          </div>
        </div>
      </div>
<a href="Python-plot-WXN.html" class="navigation navigation-prev " aria-label="Previous page"><i class="fa fa-angle-left"></i></a>
<a href="py3-pandas-ct.html" class="navigation navigation-next " aria-label="Next page"><i class="fa fa-angle-right"></i></a>
    </div>
  </div>
<script src="libs/gitbook-2.6.7/js/app.min.js"></script>
<script src="libs/gitbook-2.6.7/js/lunr.js"></script>
<script src="libs/gitbook-2.6.7/js/plugin-search.js"></script>
<script src="libs/gitbook-2.6.7/js/plugin-sharing.js"></script>
<script src="libs/gitbook-2.6.7/js/plugin-fontsettings.js"></script>
<script src="libs/gitbook-2.6.7/js/plugin-bookdown.js"></script>
<script src="libs/gitbook-2.6.7/js/jquery.highlight.js"></script>
<script>
gitbook.require(["gitbook"], function(gitbook) {
gitbook.start({
"sharing": {
"github": false,
"facebook": false,
"twitter": false,
"google": false,
"weibo": false,
"instapper": false,
"vk": false,
"all": ["facebook", "google", "twitter", "weibo", "instapaper"]
},
"fontsettings": {
"theme": "white",
"family": "sans",
"size": 2
},
"edit": {
"link": null,
"text": null
},
"download": ["Py3_course.pdf"],
"toc": {
"collapse": "subsection"
}
});
});
</script>

</body>

</html>
